package Action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.coursedao;
import Dao.noticedao;
import Dao.scheduledao;
import Dao.selectiondao;
import Entity.Course;
import Entity.Notice;
import Entity.Room_ced;
import mapper.Coursemapper;
import mapper.Noticemapper;
import mapper.Schedulemapper;
import mapper.Selectionmapper;

/**
 * Servlet implementation class smessServlet
 */
@WebServlet("/smessServlet")
public class smessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public smessServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String cid=request.getParameter("cid");
		String oid =request.getParameter("oid");
		Coursemapper cm = new coursedao();
		Course course = cm.getCourse(cid);
		String t_id = course.getId();
		Selectionmapper sm = new selectiondao();
		List<String> ls=sm.courseRelatedStudents(cid);
		Schedulemapper slm = new scheduledao();
		Room_ced rc = slm.getSchedule(cid);
		String context1 ="对不起，由于特殊原因（场地占用），您所选的将于"+rc.getDate()+"日期第"+rc.getOrder()+"节在"+rc.getRoom_Id()+"机房进行的课程"+rc.getCourse_Id()+"已被取消，请关注后续消息！";
		String context2="对不起，由于特殊原因（场地占用），您所预定将于"+rc.getDate()+"日期第"+rc.getOrder()+"节在"+rc.getRoom_Id()+"机房进行的课程"+rc.getCourse_Id()+"已被取消，请关注后续消息！";
		Notice n =new Notice();
		context1.replace("\n", "<br>");
		context2.replace("\n", "<br>");
		Noticemapper nm = new noticedao();
		n.setFrom_Id(oid);
		n.setTo_Id(t_id);
		n.setContent(context2);
		nm.sendMessage(n);
		for(int i=0;i<ls.size();i++){
	        n.setContent(context1);
			String user_id=ls.get(i);
			n.setTo_Id(user_id);
		    boolean a =nm.sendMessage(n);
		}
        
       
	}

}
